package com.hmdp.interception;

import com.hmdp.mapper.ShopMapper;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@RequiredArgsConstructor
@Component
public class ShopBloomInitRunner implements CommandLineRunner {

    private final RedissonClient redissonClient;

    private final ShopMapper shopMapper;

    @Override
    public void run(String... args) throws Exception {
        CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter("shopBloomFilter");
                bloomFilter.tryInit(500, 0.03);
                List<Long> shopIds = shopMapper.getShopIdList();
                for (Long shopId : shopIds) {
                    bloomFilter.add(shopId);
                }
            }
        });
    }
}
